//
// Created by zhou on 2021/10/18.
//
#include <iostream>
using namespace std;
struct ListNode{
    int val;
    ListNode *next;
    ListNode():val(0),next(nullptr){}
    ListNode(int x):val(x),next(nullptr){}
    ListNode(int x,ListNode *next):val(x),next(next){}
};
class Solution{
public:
    ListNode* removeElements(ListNode* head,int val){
//        if(head== nullptr)return nullptr;
        while(head!= nullptr&&head->val==val){
            ListNode *tmp =head;
            head=head->next;
            delete tmp;
        }
        ListNode* p=head;
        while(p!= nullptr&&p->next!= nullptr) {
            if (p->next->val == val) {
                ListNode *tmp = p->next;
                p = p->next->next;
                delete tmp;
            } else {
                p = p->next;
            }
        }
        return head;
    }
};
int main(){
    ListNode* p1=new ListNode(0);
    ListNode *p2=new ListNode(1);
    ListNode *p3=new ListNode(2);
    ListNode *p4=new ListNode(3);
    p1->next=p2;
    p2->next=p3;
    p3->next=p4;
    Solution solution;
    ListNode *p5=solution.removeElements(p1,1);
    cout<<p5->val;
    return 0;

}
